08. Software Control

Take control of the GPIO pin

The low level hardware interface for the GPIO pins is controlled in user space through a pseudo file system in Linux called sysfs. Control of the pin must first be "exported" to user space from kernel space. After that, the direction and pin value can be accessed like any other file. On the Jetson TX2, pin 18 corresponds to gpio481, so the number that must be exported to control it is 481 .

Begin by moving to sudo control and exporting the GPIO sysfs number. To control pin 18 of J21 on the Jetson TX2 Development board, the number is 481.

$ sudo -s
# cd /sys/class/gpio
# echo 481 >export

If you list the contents of the directory at this point, there is now a new directory named gpio481, and under it are a number of virtual files to control pin 18, including direction, to control whether the pin is used as an input or output (in or out) and a value file to control or read the voltage as high or low (1 or 0).
To toggle the pin, declare it as an output and then output the value alternately as high or low:

# echo out >gpio481/direction
# echo 1 >gpio481/value
# echo 0 >gpio481/value
# echo 1 >gpio481/value
# echo 0 >gpio481/value

When a 1 is echoed, the light will come on, and when a 0 is echoed, it will turn off! Software control has been achieved!

More GPIO pins

The same procedure can be used to control other GPIO pins. The following is a table of sysfs and pin mappings.